package ink.waita.blog.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.BlockingQueue;

/**
 * create by aries
 * 图片上传工具类
 */
@Component
public class UploadPictureUtil {

    @Value("blog.ftp.host")
    private String ftpHost;
    @Value("blog.ftp.port")
    private int ftpPort;
    @Value("blog.ftp.username")
    private String ftpUsername;
    @Value("blog.ftp.password")
    private String ftpPassword;
    @Value("blog.ftp.dataPath")
    private String ftpFilePath;
    @Value("blog.ftp.ossHostAndPort")
    private String ossHostAndPort;
    @Value("blog.ftp.ftpConnectionPoolSize")
    private int ftpPoolSize;

    private volatile BlockingQueue<FTPClient> ftpClients;

    private FTPClient getFtpClient() throws Exception {

        if (this.ftpClients == null) {
            synchronized (UploadPictureUtil.class) {
                if (ftpClients == null) {
                    for (int i = 0; i < ftpPoolSize; i++) {

                        FTPClient ftpClient = new FTPClient();
                        try {
                            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
                            ftpClient.login(ftpUsername, ftpPassword);// 登陆FTP服务器
                            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                                ftpClient.disconnect();
                                throw new RuntimeException("FTP链接失败");
                            } else {
                                ftpClients.add(ftpClient);
                            }
                        } catch (Exception e) {
                            ftpClient.disconnect();
                        }

                    }
                }
            }
        }
        return ftpClients.take();
    }

    /**
     * 上传图片到ftp服务器上
     *
     * @param inputStream 图片输入流
     * @param fileName    图片名
     * @return 图片url
     */
    public String uploadPicToFtpServer(InputStream inputStream, String fileName) throws Exception {

        FTPClient ftpClient = this.getFtpClient();
        String fileUrl = this.ossHostAndPort + ftpFilePath + fileName;
        try {
            int reply;
            reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                return null;
            }
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(this.ftpFilePath);

            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.ftpClients.add(ftpClient);
        }

        return fileUrl;
    }

}
